package com.blue.one.one4s

import cats.implicits._
import io.circe.syntax._
import io.circe.generic.auto._
import org.http4s.implicits._
import cats.effect._
import io.circe.Json
import org.http4s._
import org.http4s.circe._
import org.http4s.dsl.Http4sDsl
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import org.http4s.dsl.io._
import org.log4s.getLogger
case class User(username: String)
case class Ret(code:Int,msg:String="",error:String="")

class HelloWorldService[F[_]: Effect] extends Http4sDsl[F] {

  private[this] val logger = getLogger
  implicit val decoder = jsonOf[F, User]

  val service: HttpService[F] = {
    HttpService[F] {
      case GET -> Root / "hello" / name =>
        Ok(
          Json.obj("code" -> Json.fromInt(200), "msg" -> Json.fromString(s"Hello, ${name}"))
        )
      case req @ POST -> Root / "tweets" =>
        val user = req.as[User]
        user.flatMap( u => 
            Ok(Ret(200,"" + u.username).asJson))
    }
  }
}
